package com.example.rtmppusher.push;

import com.example.rtmppusher.util.TaskQueue;

public class RtmpPusher {

    static {
        System.loadLibrary("rtmppusher");
    }

    public final TaskQueue taskQueue = TaskQueue.get();

    //********************************************  Java接口  ********************************************

    //初始化
    public void initialize() {
        String tag = RtmpPusher.class.getSimpleName();
        native_initialize(tag);
    }

    //创建推流器
    public void prepare(String url) {
        taskQueue.add(() -> {
            listener.beforePrepare();
            if (!native_is_prepared())
                native_prepare(url);
            listener.onPrepared();
        });
    }

    //释放推流器
    public void release() {
        taskQueue.add(() -> {
            listener.beforeRelease();
            if (!native_is_released())
                native_release();
            listener.onReleased();
        });
    }

    //恢复推流
    public void resume() {
        native_resume();
    }

    //暂停推流
    public void pause() {
        native_pause();
    }

    //设置音频编码信息
    public void setAudioEncodeInfo(int sampleRate, int channels) {
        native_set_audio_encode_info(sampleRate, channels);
    }

    //设置视频编码信息
    public void setVideoEncodeInfo(int width, int height, int fps, int bitrate) {
        native_set_video_encode_info(width, height, fps, bitrate);
    }

    //推送音频帧数据
    public void pushAudioFrame(byte[] bytes) {
        native_push_audio_frame(bytes);
    }

    //推送视频帧数据
    public void pushVideoFrame(byte[] bytes) {
        native_push_video_frame(bytes);
    }

    //获取每次编码的音频样本数
    public int getInputSamples() {
        return native_get_input_samples();
    }

    //环境是否准备完成
    public boolean isPrepared() {
        return native_is_prepared();
    }

    //环境是否已释放
    public boolean isReleased() {
        return native_is_released();
    }

    //环境是否正在准备中
    public boolean isPreparing() {
        return native_is_preparing();
    }

    //环境是否正在释放中
    public boolean isReleasing() {
        return native_is_releasing();
    }

    //是否正在推流中
    public boolean isPushing() {
        return native_is_pushing();
    }

    //********************************************  Native接口  ********************************************

    native void native_initialize(String tag);

    native void native_prepare(String url);

    native void native_release();

    native void native_resume();

    native void native_pause();

    native void native_set_audio_encode_info(int sampleRate, int channels);

    native void native_set_video_encode_info(int width, int height, int fps, int bitrate);

    native int native_get_input_samples();

    native void native_push_audio_frame(byte[] bytes);

    native void native_push_video_frame(byte[] bytes);

    native boolean native_is_prepared();

    native boolean native_is_released();

    native boolean native_is_preparing();

    native boolean native_is_releasing();

    native boolean native_is_pushing();

    //********************************************  JNI回调  ********************************************

    public void onError(int code, String message) {
        listener.onError(code, message);
    }

    //********************************************  Java回调  ********************************************

    public interface EventListener {

        default void beforePrepare() {}

        default void beforeRelease() {}

        default void onPrepared() {}

        default void onReleased() {}

        default void onError(int code, String message) {}
    }

    EventListener listener = new EventListener() {};

    public void setEventListener(EventListener listener) {
        this.listener = listener;
    }
}

